<?php
namespace app\datamanage\controller;
use think\Controller;
use think\Db;
/**
 *
 */
class Jren extends Logincheck
{
    //显示页面
    function showJren($teacase='',$teaarea='')
    {

        if($teacase==''&& $teaarea==''){
            //返回茶类
            $retval = getTeaCase();
            $this->assign('teacase', $retval);
            $this->assign('data', '');
            $this->assign("currentteacase", $teacase);
            $this->assign("currentteaarea", $teaarea);
            $this->assign('areas','');
            return $this->fetch();
        }

        //返回茶类
        $retval = getTeaCase();
        $this->assign('teacase', $retval);
        //根据传参选择返回数据
        if($teacase==10000){
            $retval = model("Jren")->showJren();
        }else if($teacase==9999){
            $condition = ['bangdan_id'=>4];
            $retval = model("Jren")->showJren($condition);
        }else{
            if($teaarea==9999){
                $condition = ['teacase_id'=>$teacase, 'bangdan_id'=>4];
                $retval = model("Jren")->showJren($condition);
            }else if($teaarea==10000){
                $condition = ['teacase_id'=>$teacase];
                $retval = model("Jren")->showJren($condition);
            }else{
                $condition = ['teacase_id'=>$teacase, 'teaarea_id'=>$teaarea];
                $retval = model("Jren")->showJren($condition);
            }
        }

        $tid_name = teacase_ids();
        $aid_name = teaarea_ids();
        $rank = 1;
        foreach ($retval as &$oneres) {
            $oneres['rank'] = $rank;
            $rank++;
            $oneres['teacase'] = $tid_name[$oneres['teacase_id']];
            $oneres['teaarea'] = $aid_name[$oneres['teaarea_id']];

            // 工作年限
            // $oneres['seniority'] = $this->seniority($oneres['detailintro']);
            $seniority = $this->seniority($oneres['detailintro']);

            //荣誉得分
            $honor_point = $this->honorPoint($oneres['briefintro']);
            if($honor_point==0){
                $honor_point = $this->honorPoint($oneres['detailintro']);
            }

            //社会认可度得分
            $recognition_point = $this->recognition($oneres['briefintro']);
            if($recognition_point==0){
                $recognition_point = $this->recognition($oneres['detailintro']);
            }

            //社会身份得分
            $socialIdentity = $this->socialIdentity($oneres['briefintro']);
            if($socialIdentity ==0){
                $socialIdentity = $this->socialIdentity($oneres['detailintro']);
            }

            //茶人传
            $biography = $this->biography($oneres['biography']);

            // 访问量得分
            $click_point = $oneres['click']*10;

            //总得分
            $oneres['total_point'] = $seniority + $honor_point + $recognition_point + $socialIdentity + $biography + $click_point;

            //随机数
            $oneres['random'] = rand(1,300)*10;

        }

        $this->assign("currentteacase", $teacase);
        $this->assign("currentteaarea", $teaarea);
        $this->assign('data',$retval);

        $retval = $this->reTeaArea($teacase);
        $this->assign('areas',$retval);
        return $this->fetch();
    }

    //更新所有数据
    function updateAll()
    {
        $retval = model("Jren")->showJren();
        foreach ($retval as &$oneres) {
            // 工作年限
            $oneres['seniority'] = $this->seniority($oneres['detailintro']);
            //荣誉得分
            $oneres['honor_point'] = $this->honorPoint($oneres['briefintro']);
            if($oneres['honor_point']==0){
                $oneres['honor_point'] = $this->honorPoint($oneres['detailintro']);
            }
            //社会认可度得分
            $oneres['recognition_point'] = $this->recognition($oneres['briefintro']);
            if($oneres['recognition_point']==0){
                $oneres['recognition_point'] = $this->recognition($oneres['detailintro']);
            }
            //社会身份得分
            $oneres['socialIdentity'] = $this->socialIdentity($oneres['briefintro']);
            if($oneres['socialIdentity'] ==0){
                $oneres['socialIdentity'] = $this->socialIdentity($oneres['detailintro']);
            }

            //茶人传
            $oneres['biography_point'] = $this->biography($oneres['biography']);

            //随机分
            $random = rand(1,30)*10;

            //总得分
            $oneres['total_point'] = $oneres['seniority'] + $oneres['honor_point'] + $oneres['recognition_point'] + $oneres['socialIdentity'] + $oneres['biography_point'] + $random;
            //随机数
            $oneres['random'] = rand(0,30)*10;

            $id = $oneres['id'];
            $score = $oneres['total_point'];
            model('Jren')->updateAll($id, $score);
            $oneres['score']=$oneres['total_point'];
        }

        // $this->assign('data',$retval);
        return $this->redirect('showJren');
    }

    function modify($id, $newscore)
    {
        model('Jren')->updateAll($id, $newscore);
        return $newscore;
    }


    //返回某茶类对应的产地JSON数据
    function reTeaAreaJson($teacase_id)
    {
        // $data = $this->reTeaArea($teacase_id);
        $data = $this->reTeaArea($teacase_id);
        return json_encode($data);
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
        $retval = model("Jren")->reTeaArea($teacase_id);
        $areas = getTeaArea();
        $data = [];
        foreach ($retval as $ret) {
            foreach ($areas as $area) {
                if($ret["teaarea_id"]==$area["id"]){
                    $data[] = $area;
               }
           }
       }
       return $data;
    }


    function havebiography($id, $score, $biography)
    {
        $data = ['score'=>$score, 'biography'=>$biography];
        db('jiangren')->where('id', $id)->update($data);
        return 1;
    }

    /**********************************************************************/
    //计算工作年限
    function seniority($string)
    {
        // $pattern = '/出生于(\d{4})/';
        $pattern = '/(\d{4})/';
        preg_match_all($pattern, $string, $born);
        if(!$born[1]){
            return 0;
        }
        $now = date("Y");
        $cyear = $now - $born[1][0];
        if($cyear <0){
            $cyear=1;
        }
        $point = Ingenuity_year_calculation($cyear);
        return $point;
    }


    //计算荣誉得分
    function honorPoint($string)
    {
        //国家部委2000 省厅级1000 市局级500 县局级300 封顶3000
        $point = 0;
        $string = $string;

        $pattern = "/国家.{0,22}委/";//一个中文算三个字符，22可以匹配七个字符
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point1 = 2000;
            $point += $point1;
            // echo "国委:".$point1."<br>";
        }

        $pattern = "/省.{0,22}委/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point2 = 1000;
            $point += $point2;
            // echo "省委:".$point2."<br>";
        }

        $pattern = "/市.{0,22}委/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point3 = 500;
            $point += $point3;
            // echo "市委:".$point3."<br>";
        }

        $pattern = "/县.{0,22}委/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point4 = 300;
            $point += $point4;
            // echo "县委:".$point4."<br>";
        }

        if($point > 3000){
            $point = 3000;
        }
        return $point;
    }

    //社会认可度
    function recognition($string)
    {
        //高级1500 副高级1000 中级750
        $point = 0;
        $string = $string;

        $pattern = "/中级/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point1 = 750;
            $point += $point1;
        }

        $pattern = "/副高级/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point2 = 1000;
            $point += $point2;
        }else{
            $pattern = "/高级/";
            $reg = $this->reg($pattern, $string);
            if($reg){
                $point3 = 1500;
                $point += $point3;
            }
        }

        $pattern = "/评茶/";
        $reg1 = $this->reg($pattern, $string);
        $pattern = "/加工/";
        $reg2 = $this->reg($pattern, $string);
        $pattern = "/制茶/";
        $reg3 = $this->reg($pattern, $string);
        if($reg1 || $reg2 || $reg3){
            $point4 = 750;
            $point += $point4;
        }

        $pattern = "/大师/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point5 = 1000;
            $point += $point5;
        }

        $pattern = "/王/";
        $reg = $this->reg($pattern, $string);
        if($reg){
            $point6 = 1000;
            $point += $point6;
        }

        if($point > 1500){
            $point = 1500;
        }
        return $point;
    }

    // 社会身份
    function socialIdentity($string)
    {
        $point = 0;
        $string = $string;

        $pattern = "/会员/";
        $reg = $this->reg($pattern, $string);
        if($reg ){
            $point1 = 100;
            $point += $point1;
        }

        $pattern = "/理事/";
        $reg = $this->reg($pattern, $string);
        if($reg ){
            $point2 = 200;
            $point += $point2;
        }

        $pattern = "/副会长/";
        $reg = $this->reg($pattern, $string);
        if($reg ){
            $point3 = 500;
            $point += $point3;
        }else{
            $pattern = "/会长/";
            $reg = $this->reg($pattern, $string);
            if($reg ){
                $point4 = 2000;
                $point += $point4;
            }
        }

        if($point > 2000){
            $point = 2000;
        }
        return $point;
    }

    //茶人传
    function biography($biography)
    {
        $point = 0;
        if($biography==1){
            $point = 10000;
        }
        return $point;
    }

    //正则匹配
    function reg($pattern, $string)
    {
        // preg_match ( string $pattern , string $subject) 参数1：模式, 参数2：字符串
        if(preg_match($pattern, $string)){
            return 1;
        }else{
            return 0;
        }
    }







}



 ?>